/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Assignment;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ryuju
 */
@Stateless
public class AssignmentFacade extends AbstractFacade<Assignment> {
    @PersistenceContext(unitName = "PASS-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AssignmentFacade() {
        super(Assignment.class);
    }
    
    public List<Assignment> findAssignmentByStartEndDate()
    {
        Date curDate = new Date(System.currentTimeMillis());
        
        List<Assignment> assList = findAll();
        ArrayList<Assignment> ret = new ArrayList<Assignment>();
        
        for(Assignment ass : assList)
        {
            Date startDate = ass.getStartDate();
            Date endDate = ass.getEndDate();
            if(curDate.after(startDate) && curDate.before(endDate))
            {
                ret.add(ass);
            }
        }
        return ret;
    }
    
    public Assignment findAssignmentByID(Long id){
        List<Assignment> assList = findAll();
        for(Assignment ass : assList){
            if(ass.getId().equals(id)) return ass;
        }
        return null;
    }
    
}
